<template>
  <div class="container">
    <el-card style="margin-top: 20px">
      <div slot="header" class="clearfix">
        <span class="account">说明：目前支持学科和关键字条件筛选</span>
        <el-button
          style="float: right"
          type="success"
          icon="el-icon-edit"
          @click="$router.push(`/questions/new`)"
          >新增试题</el-button
        >
      </div>
      <!-- 选择区域 -->
      <QuestionsChoiceItem
        :list="list"
        :subjectList="subjectList"
        :keys="keys"
        @search="search"
        :page="page"
      />
      <!-- /选择区域 -->
      <el-alert
        :title="`数据一共${page.count}条`"
        type="info"
        show-icon
        style="margin-bottom: 20px"
      />

      <el-table :data="list" stripe style="width: 100%">
        <el-table-column prop="number" label="试题编号" width="180">
        </el-table-column>
        <el-table-column prop="subject" label="学科" width="180">
        </el-table-column>
        <el-table-column prop="catalog" label="目录"> </el-table-column>
        <el-table-column prop="questionType" label="题型">
          <template slot-scope="{ row }">
            {{ row.questionType | initType }}
          </template>
        </el-table-column>
        <el-table-column prop="question" label="题干" width="500">
          <template slot-scope="{ row }">
            {{ row.question.substr(3, row.question.length - 7) }}
          </template>
        </el-table-column>
        <el-table-column prop="addDate" label="录入时间" width="300">
          <template slot-scope="{ row }">
            {{ new Date(row.addDate) | parseTime }}
          </template>
        </el-table-column>
        <el-table-column prop="difficulty" label="难度">
          <template slot-scope="{ row }">
            {{ row.difficulty | initDifficulty }}
          </template>
        </el-table-column>
        <el-table-column prop="creator" label="录入人"> </el-table-column>
        <!-- 操作 -->
        <el-table-column label="操作" sortable="" fixed="right" width="280">
          <!-- <template slot-scope="{ row }"> -->
          <template slot-scope="{ row }">
            <el-button
              type="primary"
              icon="el-icon-view"
              circle
              plain
              @click="previewBtn(row)"
            ></el-button>
            <el-button
              type="success"
              icon="el-icon-edit"
              circle
              plain
              @click="$router.push(`/questions/new?id=${row.id}`)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              plain
              @click="delQuestion(row)"
            ></el-button>
            <el-button
              type="warning"
              icon="el-icon-check"
              circle
              plain
              @click="add(row)"
            ></el-button>
          </template>
        </el-table-column>
        <!-- /操作 -->
      </el-table>
      <!-- 分页 -->
      <el-row justify="end" style="margin-top: 20px" type="flex">
        <el-pagination
          background
          style="margin-top: 20px"
          @size-change="handleSizeChange"
          @current-change="getList"
          :current-page.sync="page.page"
          :page-sizes="[1, 2, 3, 5]"
          :page-size.sync="page.pagesize"
          layout=" sizes, prev, pager, next, jumper"
          :total="page.count"
        >
        </el-pagination>
      </el-row>
      <!-- /分页 -->
    </el-card>

    <Preview :showDialog.sync="showDialog" :previewObj="previewStr" />
    <!--  -->
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <el-alert
        :closable="false"
        title="此操作将该题目加入精选, 是否继续?"
        type="info"
        show-icon
      >
      </el-alert>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addBtn">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import QuestionsChoiceItem from '../components/questions-choice-item.vue'
import Preview from '../components/Preview.vue'
import { list, remove, choiceAdd } from '@/api/hmmm/questions'
import { simple } from '@/api/hmmm/subjects'
export default {
  name: 'Question',
  components: { QuestionsChoiceItem, Preview },
  data () {
    return {
      list: [],
      subjectList: [],
      keys: 2,
      page: {
        page: 1, // 当前页码
        pagesize: 5, // 每页显示的条数
        count: 0 // 总数
      },
      showDialog: false,
      previewStr: {},
      dialogVisible: false,
      addObj: {}
    }
  },
  created () {
    this.getList()
  },
  methods: {
    async getList () {
      const { data } = await list(this.page)
      // console.log(data)
      const res = await simple()
      // console.log(res)
      this.subjectList = res.data
      this.list = data.items
      this.page.count = data.counts
    },
    // 预览
    previewBtn (row) {
      this.showDialog = true
      this.previewStr = row
    },
    // 每页显示条数变化触发事件
    handleSizeChange (val) {
      console.log(val)
      this.getList()
      this.page.pagesize = val
      // console.log(this.page.pagesize)
    },
    // async delQuestion (row) {
    delQuestion (row) {
      this.$confirm('您确定删除该题库吗', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          // console.log(row)
          await remove(row)
          if (
            this.page.count % this.page.pagesize === 1 &&
            this.page.page > 1
          ) {
            this.page.page--
          }
          this.getList()

          this.$message.success('删除题库成功')
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // try {
    //   this.$confirm('您确定删除该题库吗', {
    //     confirmButtonText: '确定',
    //     cancelButtonText: '取消'
    //   }).then(() => {
    //     console.log(11)
    //   }).catch((e) => {
    //     this.$message.error('删除题库失败')
    //   })
    //   // console.log(row)
    //   await remove(row)
    //   if (this.page.count % this.page.pagesize === 1 && this.page.page > 1) {
    //     this.page.page--
    //   }
    //   this.getList()

    //   this.$message.success('删除题库成功')
    // } catch (error) {
    //   console.log('取消')
    //   this.$message.error('删除题库失败')
    // }

    add (row) {
      this.dialogVisible = true
      this.addObj = row
      // console.log(this.addObj)
      this.getList()
    },
    async addBtn () {
      try {
        this.addObj.choiceState = 1
        await choiceAdd(this.addObj)
        this.$message.success('精选加入成功')
      } catch (e) {
        this.$message.error('精选加入失败')
      }
      this.dialogVisible = false
    },
    search (data) {
      this.list = data
      this.page.count = data.length
    }
  }
}
</script>

<style scoped lang="less">
.account {
  font-size: 12px;
  color: rgb(255, 192, 203);
  margin: 20px 30px;
}
</style>
